//
// Created by francklinson on 2022/1/1 AT 14:40.
//
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
#include <algorithm>
#include <functional>

using namespace std;

class Solution {
public:
    vector<int> targetIndices(vector<int> &nums, int target) {
        sort(nums.begin(), nums.end());
        auto left = lower_bound(nums.begin(), nums.end(), target);
        auto right = lower_bound(nums.begin(), nums.end(), target + 1);
        vector<int> ans;
        if (left == nums.end())
            return ans;
        else {
            for (auto i = left; i < right; ++i) {
                ans.push_back(i - nums.begin());
            }
            return ans;
        }
    }
};

class Solution2 {
public:
    vector<int> targetIndices(vector<int> &nums, int target) {
        int cnt1 = 0;   // 小于 target 的元素数量
        int cnt2 = 0;   // 等于 target 的元素数量
        for (const int num: nums) {
            if (num < target) {
                ++cnt1;
            } else if (num == target) {
                ++cnt2;
            }
        }
        vector<int> res;   // 下标数组
        for (int i = cnt1; i < cnt1 + cnt2; ++i) {
            res.push_back(i);
        }
        return res;
    }
};

int main() {
    vector<int> nums{1, 2, 5, 2, 3};
    Solution solution;
    vector<int> ans = solution.targetIndices(nums, 2);
    for (const int &x:ans) cout << x << endl;
    return 0;
}

